﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataEntity;
using DataAccess;

namespace BLL
{
   public static class BllGoods
    {
       private static DataContext db;
       /// <summary>
       /// 得到上个月的库存
       /// </summary>
       /// <param name="goodsid"></param>
       /// <param name="pointdate"></param>
       /// <returns></returns>
        public static int GetLastMonthEndStock(int goodsid,DateTime pointdate)
        {
            DateTime CurrentMonthFirstDay = BllCommon.GetMonthFirstDay(pointdate);
            var countPurchase = GetDatePrePurchase(goodsid, CurrentMonthFirstDay).Select(n => n.Quantity).Sum();
            var countRequisition = GetDatePreRequisition(goodsid, CurrentMonthFirstDay).Select(n => n.Quantity).Sum();
            return countPurchase - countRequisition;

        }
       /// <summary>
       /// 本月入库数
       /// </summary>
       /// <param name="goodsid"></param>
       /// <param name="pointdate"></param>
       /// <returns></returns>
        public static int GetCurrentMonthPurchaseCount(int goodsid, DateTime pointdate)
        {
            DateTime CurrentMonthFirstDay = BllCommon.GetMonthFirstDay(pointdate);
            DateTime CurrentMonthLastDay = BllCommon.GetMonthLastDay(pointdate);
            var countPurchase = GetTwoDatePurchase(goodsid, CurrentMonthFirstDay, CurrentMonthLastDay).Select(n => n.Quantity).Sum();
            return countPurchase;

        }
       /// <summary>
       /// 本月入库总金额
       /// </summary>
       /// <param name="goodsid"></param>
       /// <param name="pointdate"></param>
       /// <returns></returns>
        public static  decimal GetCurrentMonthPurchaseTotalPrice(int goodsid, DateTime pointdate)
        {
            DateTime CurrentMonthFirstDay = BllCommon.GetMonthFirstDay(pointdate);
            DateTime CurrentMonthLastDay = BllCommon.GetMonthLastDay(pointdate);
            var countPurchase = GetTwoDatePurchase(goodsid, CurrentMonthFirstDay, CurrentMonthLastDay).Sum(n => n.Quantity * n.AfterTaxPrice);
            return countPurchase;

        }
       /// <summary>
        /// 本月出库数
       /// </summary>
       /// <param name="goodsid"></param>
       /// <param name="pointdate"></param>
       /// <returns></returns>
        public static int GetCurrentMonthRequisitionCount(int goodsid, DateTime pointdate)
        {
            DateTime CurrentMonthFirstDay = BllCommon.GetMonthFirstDay(pointdate);
            DateTime CurrentMonthLastDay = BllCommon.GetMonthLastDay(pointdate);
            var countRequisition = GetTwoDateRequisition(goodsid, CurrentMonthFirstDay, CurrentMonthLastDay).Select(n => n.Quantity).Sum();
            return countRequisition;

        }
       /// <summary>
       /// 得到本月出库金额
       /// </summary>
       /// <param name="goodsid"></param>
       /// <param name="pointdate"></param>
       /// <returns></returns>
        public static decimal GetCurrentMonthRequisitionTotalPrice(int goodsid, DateTime pointdate)
        {
            DateTime CurrentMonthFirstDay = BllCommon.GetMonthFirstDay(pointdate);
            DateTime CurrentMonthLastDay = BllCommon.GetMonthLastDay(pointdate);
            var countRequisition = GetTwoDateRequisition(goodsid, CurrentMonthFirstDay, CurrentMonthLastDay).Sum(n => n.Quantity * n.AfterTaxPrice);
            return countRequisition;

        }
       /// <summary>
        /// 某个时间点之前的入库明细
       /// </summary>
       /// <param name="goodsid"></param>
       /// <param name="pointdate"></param>
       /// <returns></returns>
        public static List<OffPurchaseBillDetail> GetDatePrePurchase(int goodsid,DateTime pointdate)
        {
            db = new DataContext();
            var result = new List<OffPurchaseBillDetail>();
            result = db.OffPurchaseBillDetails.Where(n => n.OffPurchaseBill.StorageTime < pointdate&&n.OffGoodsID==goodsid).ToList();
            return result;
        }
       /// <summary>
       /// 某个时间点之前的出库明细
       /// </summary>
       /// <param name="goodsid"></param>
       /// <param name="pointdate"></param>
       /// <returns></returns>
        public static List<OffRequisitionBillDetail> GetDatePreRequisition(int goodsid, DateTime pointdate)
        {
            db = new DataContext();
            var result = new List<OffRequisitionBillDetail>();
            result = db.OffRequisitionBillDetails.Where(n => n.OffRequisitionBill.OutBoundTime < pointdate && n.OffGoodsID == goodsid).ToList();
            return result;
        }
       /// <summary>
       /// 两个时间之间的入库明细
       /// </summary>
       /// <param name="goodsid"></param>
       /// <param name="beginDate"></param>
       /// <param name="endDate"></param>
       /// <returns></returns>
        public static List<OffPurchaseBillDetail> GetTwoDatePurchase(int goodsid, DateTime beginDate, DateTime endDate)
        {
            db = new DataContext();
            var result = new List<OffPurchaseBillDetail>();
            result = db.OffPurchaseBillDetails.Where(n => n.OffPurchaseBill.StorageTime >= beginDate && n.OffGoodsID == goodsid && n.OffPurchaseBill.StorageTime <= endDate).ToList();
            return result;
        }
       /// <summary>
       /// 两个时间之间的出库明细
       /// </summary>
       /// <param name="goodsid"></param>
       /// <param name="beginDate"></param>
       /// <param name="endDate"></param>
       /// <returns></returns>
        public static List<OffRequisitionBillDetail> GetTwoDateRequisition(int goodsid, DateTime beginDate, DateTime endDate)
        {
            db = new DataContext();
            var result = new List<OffRequisitionBillDetail>();
            result = db.OffRequisitionBillDetails.Where(n => n.OffRequisitionBill.OutBoundTime>=beginDate && n.OffGoodsID == goodsid&&n.OffRequisitionBill.OutBoundTime<=endDate).ToList();
            return result;
        }
    }
}
